package run.core;
import java.util.ArrayList;
import java.util.List;

import run.Cell;
import run.Facet;
import run.StrFacet;

/** String Cell
 * 
 * @author Rajesh.ramakrishnan
 */
public class STRLIT extends Cell {
	private Facet in;
	private StrFacet out;
	private String myValue;
	public STRLIT(String name) {
		super("STR", name);
		myValue = name;
		in = new Facet(".in", this, INPUT_FACET);
		out = new StrFacet(".out", this, OUTPUT_FACET, name);
	}

	@Override
	public List<Facet> stimulate(Facet stimmer)
	{
		ArrayList<Facet> doneFacets = new ArrayList<Facet>();
		List<Object> vals = in.dispatchOutbound();
		
		Object concat = vals.get(0);
		if (!(concat instanceof NIL)) {
			System.out.println(myValue + ":" + concat);
			out.concat(concat);
			doneFacets.add(out);
		}
			
		return doneFacets;
	}
	
	protected String injectInt(String s, int val) { return s.replaceFirst("%d", ((Integer)val).toString()); }
	protected String inject(String s, Object o) { return s.replaceFirst("%s", o.toString()); }
	
	/** Find and fix special characters.
	 * 
	 * @author Rajesh.ramakrishnan
	 */
	protected List<Object> process(String nm) {
		ArrayList<Object> vals = new ArrayList<Object>();
		String tmp = nm.replaceAll("\\\\n", "\n");
		tmp = tmp.replaceAll("\\\\t", "\t");
		tmp = tmp.replaceAll("\\\\b", "\b");
		tmp = tmp.replaceAll("\\\\f", "\f");
		tmp = tmp.replaceAll("\\\\r", "\r");
		tmp = tmp.replaceAll("\\\\u", "\r");
		tmp = tmp.replaceAll("\\\\%", "%");
		vals.add(tmp);
		return vals;
	}

	//Dot printout method
	public String toDot()
	{
		String returnString = "";
		returnString += "subgraph " + hashCode() + "{\n";
		returnString += "label = \"" + cellTypeName + "\";\n";
		returnString += "}\n";
		return returnString;
	}
	
	
	public List<Facet> getFacets()
	{
		ArrayList<Facet> temp = new ArrayList<Facet>();
		temp.add(in);
		temp.add(out);
		return temp;
	}
		
	public List<Facet> getOutFacets() {
		ArrayList<Facet> arrayList = new ArrayList<Facet>();
		arrayList.add(out);
		return arrayList;
	}
	
	@Override
	public boolean isLiteral() {
		return true;
	}
}
